import { Controller } from "../../mvc/core/Controller";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { ISmeltModule } from "../../../modules/interfaces/ISmeltModule";
import { IView } from "../../mvc/interfaces/IView";
import { SmeltModule } from "../../../modules/SmeltModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { SmeltPlanData } from "../view/smelt/data/SmeltPlanData";
import { GainItemUtil } from "../view/common/util/GainItemUtil";
import { GameProxy } from "../mvc/GameProxy";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { ItemData } from "../resource/info/ItemData";
import { SmeltConfig } from "../resource/SmeltConfig";
import { Global } from "../../../com/gengine/global/Global";
import { SmeltMaterialItemData } from "../view/smelt/data/SmeltMaterialItemData";
type int = number;
//class SmeltController
    
    export  class SmeltController extends Controller
    {
       

        /* internal  */checkCanSmelt(): Boolean
        {
            var loc1=0;
            var loc2=0;
            if (this._currentSmeltPlan == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69206));
                return false;
            }
            if (!this.isEnoughMaterial()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69207));
                return false;
            }
            if (!this.isEnoughMoney()) 
            {
                loc1 = cache.role.money.coinBind + cache.role.money.coin;
                loc2 = this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc2 - loc1));
                MsgManager.showRollTipsMsg(Language.getString(69208));
                return false;
            }
            return true;
        }

        /* internal  */onModuleShow(arg1: Object /* flash.events.Event */): void
        {
            (view as ISmeltModule).checkCategoryTime();
            if (this._isBagDataChanged) 
            {
                this._isBagDataChanged = false;
                this.serverBagDataChangehandler();
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._smeltModule) 
            {
                this._smeltModule = new SmeltModule();
                this._smeltModule.addEventListener(WindowEvent.SHOW, this.onModuleShow);
                this.onCountPlanListNum();
                this.doChangeSmeltPlan(this._currentSmeltPlan);
            }
            return this._smeltModule;
        }

        public get currentMaxSmeltNum(): int
        {
            if (this._currentMaxSmeltNum < 1) 
            {
                return 1;
            }
            if (this._currentMaxSmeltNum > 999) 
            {
                return 999;
            }
            return this._currentMaxSmeltNum;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.SmeltSmeltButtonClick, this.onSmeltButtonClick);
            Dispatcher.addEventListener(EventName.SmeltCountPlanListNum, this.onCountPlanListNum);
            Dispatcher.addEventListener(EventName.SmeltSelectedPlanChange, this.onSmeltPlanChange);
            Dispatcher.addEventListener(EventName.SmeltStartSmelt, this.onStartSmelt);
            Dispatcher.addEventListener(EventName.SmeltSmeltSuccess, this.onSmeltSuccess);
            Dispatcher.addEventListener(EventName.SmeltSmeltFailed, this.onSmeltFailed);
            Dispatcher.addEventListener(EventName.SmeltSmeltNumChange, this.onSmeltNumChange);
            Dispatcher.addEventListener(EventName.SmeltOpenSmeltByIndex, this.onOpenSmeltByIndex);
            Dispatcher.addEventListener(EventName.SmeltOpenSmeltByMaterialData, this.onOpenSmeltByMaterialData);
            Dispatcher.addEventListener(EventName.SmeltOpenSmeltByPlanCode, this.onOpenSmeltBySmeltPlanCode);
            Dispatcher.addEventListener(EventName.SmeltShowCanSmeltOnlyChange, this.onShowCanSmeltOnlyChange);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateCost);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdate);
            return;
        }

        public get currentSmeltNum(): int
        {
            if (this._currentSmeltNum < 1) 
            {
                return 1;
            }
            if (this._currentSmeltNum > this.currentMaxSmeltNum) 
            {
                return this.currentMaxSmeltNum;
            }
            return this._currentSmeltNum;
        }

        public set currentSmeltNum(arg1: int)
        {
            this._currentSmeltNum = arg1;
            return;
        }

        /* internal  */onSmeltPlanChange(arg1: DataEvent): void
        {
            this.doChangeSmeltPlan(arg1.data as SmeltPlanData);
            return;
        }

        /* internal  */doChangeSmeltPlan(arg1: SmeltPlanData=null, arg2: Boolean=true): void
        {
            this._currentSmeltPlan = arg1;
            if (this._smeltModule == null || this._smeltModule.isHide) 
            {
                return;
            }
            if (this._currentSmeltPlan) 
            {
                this.countPlanSmeltNum(this._currentSmeltPlan);
                this._currentMaxSmeltNum = this._currentSmeltPlan.canSmeltNum;
                if (arg2) 
                {
                    this.currentSmeltNum = this._currentSmeltPlan.defaultSmeltNum;
                }
                this._smeltModule.setMaterials(this._currentSmeltPlan.materials);
                this._smeltModule.setMainProduce(this._currentSmeltPlan.mainProduce, this._currentSmeltPlan.smeltPlanDesc);
                this._smeltModule.setSuccessRate(this._currentSmeltPlan.mainProduceRate);
                this._smeltModule.setCost(this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum, this.isEnoughMoney());
                this._smeltModule.setSmeltNum(this.currentSmeltNum);
            }
            else 
            {
                this._currentMaxSmeltNum = 0;
                this._smeltModule.setMaterials([]);
                this._smeltModule.setMainProduce(null, "");
                this._smeltModule.setSuccessRate(0);
                this._smeltModule.setCost(0, true);
                this._smeltModule.setSmeltNum(1);
            }
            return;
        }

        /* internal  */onSmeltButtonClick(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onCountPlanListNum(arg1: DataEvent=null): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            if (this._smeltModule && !this._smeltModule.isHide) 
            {
                loc1 = this._smeltModule.currentShowedPlanListData;
                loc3 = 0;
                while (loc3 < loc1.length) 
                {
                    loc2 = loc1[loc3] as SmeltPlanData;
                    this.countPlanSmeltNum(loc2);
                    ++loc3;
                }
                this._smeltModule.refreshPlanList();
            }
            return;
        }

        /* internal  */onStartSmelt(arg1: DataEvent=null): void
        {
            if (this.checkCanSmelt()) 
            {
                GainItemUtil.gainItemTip(this._currentSmeltPlan.mainProduce, this.startSmeltImpl, null);
            }
            return;
        }

        /* internal  */startSmeltImpl(arg1: Object): void
        {
            GameProxy.smelt.smelt(this._currentSmeltPlan.smeltPlanCode, this.currentSmeltNum);
            return;
        }

        /* internal  */onSmeltSuccess(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(69205));
            SoundManager.instance.soundPlay(SoundTypeConst.HeCheng);
            this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            return;
        }

        /* internal  */onSmeltFailed(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onSmeltNumChange(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.currentSmeltNum = loc1;
            this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            return;
        }

        /* internal  */onOpenSmeltByIndex(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            (view as ISmeltModule).openSmeltByIndex(loc1);
            view.show();
            return;
        }

        /* internal  */onOpenSmeltByMaterialData(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            var loc2=SmeltConfig.instance.getSmeltPlanByMaterial(loc1.itemCode);
            if (loc2) 
            {
                view.show();
                (view as ISmeltModule).showToPanelByPlan(loc2, false);
            }
            return;
        }

        /* internal  */onOpenSmeltBySmeltPlanCode(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=false;
            if (arg1.data) 
            {
                loc1 = arg1.data.code as int;
                loc2 = arg1.data.isGuide;
            }
            var loc3;
            if (loc3 = SmeltConfig.instance.getPlanById(loc1)) 
            {
                view.show();
                (view as ISmeltModule).showToPanelByPlan(loc3, loc2);
            }
            return;
        }

        /* internal  */onShowCanSmeltOnlyChange(arg1: DataEvent): void
        {
            cache.smelt.isShowCanSmeltOnly = arg1.data;
            (view as ISmeltModule).updatePlanListData();
            return;
        }

        /* internal  */onRoleLevelUpdate(arg1): void
        {
            (view as ISmeltModule).updatePlanListData();
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object=null): void
        {
            if (!this._smeltModule || this._smeltModule.isHide) 
            {
                this._isBagDataChanged = true;
                return;
            }
            this.onCountPlanListNum();
            if (_view && !_view.isHide && cache.smelt.isShowCanSmeltOnly) 
            {
                (view as ISmeltModule).updatePlanListData();
            }
            if (this._currentSmeltPlan) 
            {
                this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            }
            return;
        }

        /* internal  */updateCost(arg1: Object=null): void
        {
            if (this._smeltModule && !this._smeltModule.isHide && this._currentSmeltPlan) 
            {
                this._smeltModule.setCost(this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum, this.isEnoughMoney());
            }
            return;
        }

        /* internal  */countPlanSmeltNum(arg1: SmeltPlanData): void
        {
            var loc1=0;
            var loc2=null;
            if (!arg1 && !Global.isDebugModle) 
            {
                return;
            }
            var loc3=0;
            while (loc3 < arg1.materials.length) 
            {
                loc2 = arg1.materials[loc3] as SmeltMaterialItemData;
                this.countOwnItemAmount(loc2);
                if (loc3 != 0) 
                {
                    loc1 = Math.min(loc1, Math.floor(loc2.itemAmountOwn / loc2.itemAmountNeeded));
                }
                else 
                {
                    loc1 = Math.floor(loc2.itemAmountOwn / loc2.itemAmountNeeded);
                }
                ++loc3;
            }
            arg1.canSmeltNum = loc1;
            return;
        }

        /* internal  */countOwnItemAmount(arg1: SmeltMaterialItemData): SmeltMaterialItemData
        {
            var loc2=null;
            arg1.itemAmountOwn = 0;
            var loc1=cache.pack.backPackCache.getAllItems();
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2 = loc1[loc3] as ItemData;
                if (loc2 && (loc2.itemInfo.item.code == arg1.itemBindCode || loc2.itemInfo.item.code == arg1.itemUnBindCode)) 
                {
                    arg1.itemAmountOwn = arg1.itemAmountOwn + loc2.itemAmount;
                }
                ++loc3;
            }
            return arg1;
        }

        /* internal  */isEnoughMoney(): Boolean
        {
            var loc1=0;
            if (this._currentSmeltPlan) 
            {
                loc1 = cache.role.money.coinBind + cache.role.money.coin;
                return loc1 >= this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum;
            }
            return true;
        }

        /* internal  */isEnoughMaterial(): Boolean
        {
            var loc1=null;
            var loc2=0;
            if (this._currentSmeltPlan) 
            {
                loc2 = 0;
                while (loc2 < this._currentSmeltPlan.materials.length) 
                {
                    loc1 = this._currentSmeltPlan.materials[loc2] as SmeltMaterialItemData;
                    if (loc1.itemAmountOwn < loc1.itemAmountNeeded * this.currentSmeltNum) 
                    {
                        return false;
                    }
                    ++loc2;
                }
                return true;
            }
            return false;
        }

        private /* var */_smeltModule: ISmeltModule;

        private /* var */_currentSmeltPlan: SmeltPlanData;

        private /* var */_currentMaxSmeltNum: int=1;

        private /* var */_currentSmeltNum: int=1;

        private /* var */_isBagDataChanged: Boolean=false;
    }
